# -*- coding: utf-8 -*-
from lib import LogHandler

__author__ = 'JK'
__date__ = '2019/5/9 21:44'

import yaml
import os
import json

logger = LogHandler(__name__)

class CasesManager:
    def __init__(self, path: str=None):
        self.datas = {}
        self.path = path
        if not self.path:
            self.path = 'case/'

    def get_all_cases(self) -> list:
        return os.listdir(self.path)

    def read_cases_json(self, file_name: str) -> str:
        """
            读取json中的case信息
        :return: json中的case信息
        """
        try:
            if 'json' in file_name:
                with open(self.path + file_name, encoding='utf-8') as file:
                    return json.load(file)
            elif 'yaml' or 'yml' in file_name:
                with open(self.path + file_name, encoding='utf-8') as file:
                    return yaml.load(file)
            else:
                logger.error("解析测试用例时出错, 出错文件: {}".format(file_name))
                raise ("解析测试用例时出错, 出错文件: {}".format(file_name))
        except Exception as err:
            logger.error("解析测试用例时出错, 出错文件: {}, 错误信息: {}".format(file_name, err))

    def make_cases_info(self) -> list:
        """
            构造完整的测试用例信息集合
        :return:
        """
        all_cases_enum = []
        for file in self.get_all_cases():
            class_name = file.split('.')[0].title().replace("_", '')
            all_cases_enum.append({class_name: self.read_cases_json(file)})
        return all_cases_enum


if __name__ == '__main__':
    x = CasesManager()
